package org.yu.team2023.flashdays;

import com.mojang.logging.LogUtils;

import net.minecraft.network.chat.Component;

import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.slf4j.Logger;
import org.yu.team2023.flashdays.client.ModInputs;
import org.yu.team2023.flashdays.mixin.RangedAttributeAccessor;
import org.yu.team2023.flashdays.net.FlashNet;
import org.yu.team2023.flashdays.registries.SimpleReg;

// The value here should match an entry in the META-INF/mods.toml file
@Mod(FlashDays.MODID)
public class FlashDays {

    // Define mod id in a common place for everything to reference
    public static final String MODID = "flashdays";
    // Directly reference a slf4j logger
    private static final Logger LOGGER = LogUtils.getLogger();
    private final SimpleReg simpleReg;
    private static FlashDays INSTANCE;

    public static FlashDays getInstance() {
        return INSTANCE;
    }

    public static SimpleReg getSimpleReg() {
        return INSTANCE.simpleReg;
    }

    public FlashDays() {
        simpleReg = new SimpleReg(MODID);
        simpleReg.modTab(builder -> builder.title(Component.translatable("tab.flashdays.title"))
                .withTabsBefore(CreativeModeTabs.SPAWN_EGGS)
                .icon(() -> ModItems.KHANS_THRONE.getTreasure1().get().getDefaultInstance()));
        INSTANCE = this;
        FDConfig.registerConfig();
        ModItems.init();
        ModMobEffects.init();
        ModAttributes.init();
        ModEntities.init();
        ModSounds.init();
        ModBlockEntities.init();
        FlashNet.registerMessage();
        FMLJavaModLoadingContext.get().getModEventBus().addListener(EventPriority.LOWEST,this::onCommonSetup);
        DistExecutor.unsafeRunWhenOn(Dist.CLIENT,()-> ModInputs::init);
        MinecraftForge.EVENT_BUS.addGenericListener(Level.class,this::onAttachLevelCapability);

    }

    private void onCommonSetup(FMLCommonSetupEvent event){
        modAttribute(Attributes.MAX_HEALTH,Integer.MAX_VALUE);
        modAttribute(Attributes.ATTACK_DAMAGE,Integer.MAX_VALUE);
        modAttribute(Attributes.ARMOR,Integer.MAX_VALUE);
        modAttribute(Attributes.ARMOR_TOUGHNESS,Integer.MAX_VALUE);
    }
    private void onAttachLevelCapability(AttachCapabilitiesEvent<Level> event){

    }
    private void modAttribute(Attribute attribute,double max){
        if(attribute instanceof RangedAttributeAccessor accessor&&max>accessor.getMinValue()){
            accessor.setMaxValue(max);
        }
    }


}
